<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">库存管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick">被服科室库存</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex"></div>
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="部门科室">
            <el-select v-model="searchForm.area_id" placeholder="请选择部门科室" clearable filterable @change="onSubmit">
              <el-option v-for="item in deptArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-scrollbar class="table-wrap">
          <el-table :data="tableData" :border="true" :header-cell-style="tabHeadStyle" :cell-style="tabcellStyle" :row-style="rowStyle" max-height="650">
            <el-table-column prop="area" label="部门科室" align="center" min-width="130"> </el-table-column>
            <el-table-column v-for="(item, index) in tableFields" :key="index" :label="item.value" align="center">
              <template>
                <el-table-column v-for="(unit, unitIndex) in item.child" :key="unitIndex" :prop="unit.key" :label="unit.value" align="center" min-width="90"> </el-table-column>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="160" fixed="right">
              <template slot-scope="scope">
                <span class="nx-btn nx-edit" @click="handleEdit(scope.row)">查看明细</span>
                <span class="nx-btn nx-edit" @click="handleEdit(scope.row, 'stock')">库存调整</span>
              </template>
            </el-table-column>
          </el-table>
        </el-scrollbar>
      </div>
    </div>
    <AddEditDialog :addEditId="addEditId" :areaName="areaName" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog"></AddEditDialog>
    <AddEditDialog2 :addEditId="addEditId" :areaName="areaName" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog2"></AddEditDialog2>
  </div>
</template>
<script>
import NxTable from "@/components/NxTable.vue";
import AddEditDialog from "./components/addEditDialog.vue";
import AddEditDialog2 from "./components/addEditDialog2.vue";
import { Page } from "@/utils/pagination";
import { patientClothAreaInventoryList, getPatientField, delChild } from "@/api/stock";
import { getArea } from "@/api/system";

export default {
  components: {
    NxTable,
    AddEditDialog,
    AddEditDialog2,
  },
  // 表格数据
  data() {
    return {
      // 表头样式
      tabHeadStyle: {
        "background-color": "#f5f7fa",
        color: "#303133",
        "font-weight": "bold",
        "text-align": "center",
      },
      // 行样式
      tabcellStyle: {
        color: "#666",
      },
      rowStyle: {
        height: "20px",
      },
      // 查询字段
      searchForm: {
        area_id: "",
      },
      pagesForm: {
        page: 1,
        limit: 10,
      },
      // 表格字段
      tableCols: [
        {
          label: "尺码",
          prop: "size",
          align: "center",
        },
        {
          label: "备注",
          prop: "memo",
          align: "center",
        },
        {
          label: "操作",
          slot: "operation",
          align: "center",
          width: 180,
          fixed: "right",
        },
      ],
      tableData: [],
      tableFields: [],
      pagination: new Page(),
      crumbsStatus: "", // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditIsDialog2: false, // 添加编辑页面状态
      addEditStatus: "add",
      addEditId: "", // 添加编辑rowidd
      deptArray: [],
      areaName: "",
      typeDialog2: 0,
    };
  },
  // 初始化加载
  mounted() {
    this.getPatientField();

    this.getAreaList();

    // 查询
    this.getList();
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.page = 1;
      this.pagesForm.page = 1;
      this.getList();
    },
    // 分页查询
    refresh(val) {
      this.pagesForm.page = val.page;
      this.pagesForm.limit = val.limit;
      this.getList();
    },
    // 重置
    resetForm() {
      this.searchForm = {
        area_id: "",
      };
      this.pagesForm = {
        page: 1,
        limit: 10,
      };
      this.onSubmit();
    },
    // 查询列表
    getList() {
      patientClothAreaInventoryList(this.pagesForm, this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.pagination.count = res.count;
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久停用, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delChild({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消停用",
          });
        });
    },
    // 获取表头字段
    getPatientField() {
      getPatientField().then((res) => {
        if (res.code == 200) {
          this.tableFields = res.data;
        }
      });
    },
    // 获取科室列表
    getAreaList() {
      getArea().then((res) => {
        if (res.code == 200) {
          this.deptArray = res.data;
        }
      });
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog2 = 0;
      this.typeDialog = false;
      this.addEditIsDialog = false;
    },
    // 添加
    handleAdd() {
      this.addEditId = "";
      this.addEditStatus = "add";
      this.addEditIsDialog = true;
    },
    // 编辑
    handleEdit(row, status) {
      this.addEditId = row.area_id + "";
      this.areaName = row.area + "";
      this.addEditStatus = "edit";
      if (status == "stock") {
        this.addEditIsDialog2 = true;
      } else {
        this.addEditIsDialog = true;
      }
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false;
      this.addEditIsDialog2 = false;
      this.typeDialog = false;
    },
  },
};
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
